home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 July / macformat52.iso / mac / Shareware Plus / Developers / YAAF v1.0 alpha 1 / Headers / Core / XThread.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-24  |  5.2 KB  |  221 lines

  1. /*    XThread.h
  2.  *
  3.  *        This encapsulates thread support
  4.  */
  5.  
  6. /*  YAAF - Yet another application framework
  7.  *  Copyright (C) 1997 William Edward Woody and In Phase Consulting
  8.  *  
  9.  *  This library is free software; you can redistribute it
  10.  *  and/or modify it under the terms of the GNU Library
  11.  *  General Public License as published by the Free Software
  12.  *  Foundation; either version 2 of the License, or any
  13.  *  later version.
  14.  *  
  15.  *  This library is distributed in the hope that it will be
  16.  *  useful, but WITHOUT ANY WARRANTY; without even the implied
  17.  *  warranty of MERCHANTABIILITY or FITNESS FOR A PARTICULAR
  18.  *  PURPOSE. See the GNU Library General Public License for
  19.  *  more details.
  20.  *  
  21.  *  You should have received a copy of the GNU Library General
  22.  *  Public License along with this library; if not, write to the
  23.  *  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  24.  *  Boston, MA 02111-1307, USA.
  25.  *  
  26.  *  To contact the author, either e-mail me at
  27.  *  woody@alumni.caltech.edu, or write to us at
  28.  *  
  29.  *          William Edward Woody
  30.  *          In Phase Consulting
  31.  *          1545 Ard Eevin Avenue
  32.  *          Glendale, CA 91202
  33.  */
  34.  
  35. #ifndef __XTHREAD_H__
  36. #define __XTHREAD_H__
  37.  
  38. #include <XConfig.h>
  39.  
  40. #if OPT_MACOS == 1
  41. #include <Threads.h>
  42. #endif
  43.  
  44. #if defined(__MWERKS__)
  45.     #if defined(macintosh)
  46.         #pragma options align=power
  47.     #endif
  48.     #if defined(__INTEL__)
  49.         #pragma pack(push,2)
  50.     #endif
  51. #endif
  52.  
  53. /************************************************************************/
  54. /*                                                                        */
  55. /*    Forwards                                                            */
  56. /*                                                                        */
  57. /************************************************************************/
  58.  
  59. class XGThread;
  60.  
  61. /************************************************************************/
  62. /*                                                                        */
  63. /*    Thread Support                                                        */
  64. /*                                                                        */
  65. /************************************************************************/
  66.  
  67. typedef void (* XGThreadProcPtr)(XGThread *);
  68.  
  69. /*    XGThread
  70.  *
  71.  *        This is the thread object; this knows how to handle, identify,
  72.  *    fold, and mutulate a thread object.
  73.  *
  74.  *        Attach/Detach semantics provides me a way to manipulate a
  75.  *    thread, even after the thread dies. Normally, the XGThread class
  76.  *    pointer is owned by the thread--when the thread dies, the XGThread
  77.  *    class object is deleted. This sucks if you want to keep a pointer
  78.  *    to the thread object pass it's expiration.
  79.  *
  80.  *        Thus, the Attach/Detach semantics allows other objects to 
  81.  *    keep a pointer to the XGThread, and claim ownership past the
  82.  *    thread's termination.
  83.  *
  84.  *        Note that when the thread is first created, it's owned by
  85.  *    the thread itself. That means if you want to create this thread
  86.  *    and keep a pointer to that thread, you need to explicitly attach
  87.  *    to the thread after it's creation: thus,
  88.  *
  89.  *        XGThread *ptr = new XGThread(myProc,0,true);    // create suspended
  90.  *        ptr->Attach();                                    // attach to it
  91.  *        ptr->Resume();                                    // and resume execution
  92.  */
  93.  
  94. class XGThread {
  95.     public:
  96.                                 XGThread(XGThreadProcPtr, long stack = 0, 
  97.                                             bool suspend = false);
  98.                                 XGThread();
  99.         virtual                    ~XGThread();
  100.         
  101.         /*
  102.          *    Attach/detach semantics--for other threads which want
  103.          *    to know information about this thread.
  104.          */
  105.         
  106.         void                    Attach(void)
  107.                                     {
  108.                                         fAttach++;
  109.                                     }
  110.         void                    Detach(void)
  111.                                     {
  112.                                         if (--fAttach <= 0) delete this;
  113.                                     }
  114.         long                    GetAttach(void) const
  115.                                     {
  116.                                         return fAttach;
  117.                                     }
  118.         
  119.         /*
  120.          *    Thread management routines
  121.          */
  122.         
  123.         static XGThread            *CurrentThread();
  124.         static void                YieldThread(bool f = false);
  125.         
  126.         /*
  127.          *    Scheduling routines
  128.          */
  129.         
  130.         void                    Suspend(void);
  131.         void                    Resume(void);
  132.         bool                    IsSuspend(void);    // Thread suspended?
  133.         
  134.         void                    Kill(void);
  135.         bool                    IsAlive(void);        // Valid thread?
  136.  
  137.     private:
  138.         long                    fAttach;
  139.         XGThreadProcPtr            fProc;
  140.         
  141.         XGThread                *fNext;
  142.         static XGThread            *gList;
  143.  
  144. #if OPT_MACOS == 1
  145.         ThreadID                fThreadID;
  146.         static pascal void        *ThreadEntry(void *);
  147.         static pascal void        ThreadTerminate(ThreadID,void *);
  148. #endif
  149.  
  150. #if OPT_WINOS == 1
  151.         bool                    fThreadSuspend;
  152.         HANDLE                    fThreadHandle;
  153.         DWORD                    fThreadID;
  154.         
  155.         static DWORD WINAPI        ThreadEntry(LPVOID);
  156. #endif
  157. };
  158.  
  159. /*    XGSemaphore
  160.  *
  161.  *        This is a semaphore object. This allows me to create and
  162.  *    control access to a chunk of critical code
  163.  */
  164.  
  165. class XGSemaphore {
  166.     public:
  167.                                 XGSemaphore(unsigned long maxaccess = 1);
  168.         virtual                    ~XGSemaphore();
  169.         
  170.         /*
  171.          *    Access routines
  172.          */
  173.         
  174.         void                    EnterCritical(void);
  175.         void                    LeaveCritical(void);
  176.  
  177.     private:
  178.  
  179. #if OPT_MACOS == 1
  180.         unsigned long            fAccess;
  181. #endif
  182.  
  183. #if OPT_WINOS == 1
  184.         HANDLE                    fHandle;
  185. #endif
  186. };
  187.  
  188. /*    XGCritical
  189.  *
  190.  *        Access object. This provides the enter/exit semantics in
  191.  *    a stack object for semaphore access; this provides a return-safe
  192.  *    and throw-safe way of providing enter/exit semaphore access
  193.  */
  194.  
  195. class XGCritical {
  196.     public:
  197.                                 XGCritical(XGSemaphore *x)
  198.                                     {
  199.                                         fSemaphore = x;
  200.                                         x->EnterCritical();
  201.                                     }
  202.                                 ~XGCritical()
  203.                                     {
  204.                                         fSemaphore->LeaveCritical();
  205.                                     }
  206.     
  207.     private:
  208.         XGSemaphore                *fSemaphore;
  209. };
  210.  
  211. #if defined(__MWERKS__)
  212.     #if defined(macintosh)
  213.         #pragma options align=reset
  214.     #endif
  215.     #if defined(__INTEL__)
  216.         #pragma pack(pop)
  217.     #endif
  218. #endif
  219.  
  220. #endif /* __XTHREAD_H__ */
  221.